package com.pentofab.payrollmanagement.ui.controller;

import java.net.URL;
import java.util.ResourceBundle;

import com.pentofab.payrollmanagement.application.PayrollApplicationFactory;
import com.pentofab.payrollmanagement.dao.EmployeeDao;
import com.pentofab.payrollmanagement.dao.model.Company;
import com.pentofab.payrollmanagement.exception.DatabaseException;
import com.pentofab.payrollmanagement.ui.control.CustomTextField;
import com.pentofab.payrollmanagement.utilities.ValidationUtilities;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.TextField;

public class AddCompanyUIController extends AbstractUIController implements
		FormValidator {

	private Company company;
	private boolean updateMode = false;
	@FXML
	Parent root;

	@FXML
	CustomTextField companyNameField;

	@FXML
	CustomTextField codeField;

	@FXML
	CustomTextField addressLine1Field;

	@FXML
	CustomTextField addressLine2Field;

	@FXML
	CustomTextField cityField;

	@FXML
	CustomTextField stateField;

	@FXML
	CustomTextField countryField;

	@FXML
	CustomTextField zipCodeField;

	@FXML
	CustomTextField telephone1Field;

	@FXML
	CustomTextField telephone2Field;

	@FXML
	CustomTextField faxField;

	@FXML
	TextField emailField;

	@Override
	public Parent getView() {
		return root;
	}

	@Override
	public String getTitle() {
		return UIMessages.UITitles.ADD_UPDATE_COMPANY;
	}

	public void save() {
		if (!isValid()) {
			showError(getValidationMessage());
		} else {
			Company company = new Company();
			company.setName(companyNameField.getText());
			company.setCompanyID(codeField.getText());
			company.setAddress1(addressLine1Field.getText());
			company.setAddress2(addressLine2Field.getText());
			company.setCity(cityField.getText());
			company.setState(stateField.getText());
			company.setCountry(countryField.getText());
			company.setZipcode(zipCodeField.getText());
			company.setTelephone1(telephone1Field.getText());
			company.setTelephone2(telephone2Field.getText());
			company.setFax(faxField.getText());
			company.setMailId(emailField.getText());
			EmployeeDao empDao = PayrollApplicationFactory.getEmployeeMgmt();
			try {
				if (!updateMode) {
					empDao.addCompany(company);
					refreshUI();
					showInfo(String.format(UIMessages.COMPANY_ADD_SUCCESS,
							company.getName()));
				} else {
					empDao.updateCompany(company);
					showInfo(String.format(UIMessages.COMPANY_UPDATE_SUCCESS,
							company.getName()));
				}
			} catch (DatabaseException e) {
				String message = "";
				switch (e.getCode()) {
				case DatabaseException.DUPLICATE_ENTRY:
					message = UIMessages.DUPLICATE_COMPANY_CODE_ERROR_MESSAGE;
					break;
				case DatabaseException.UNKNOWN_ERROR:
				default:
					message = UIMessages.UNKNOW_DATABASE_ERROR;
					break;
				}
				showError(message);
			} catch (Exception e) {
				showError(UIMessages.UNKNOW_DATABASE_ERROR);
			}
		}
	}

	public void goBack() {
		if (getParent() != null && getParent().length() > 0) {
			PayrollApplicationFactory.getMainController().showScreen(
					getParent(), false);
		}
	}

	public void reset() {
		if (company != null) {
			refreshUI(company);
		} else {
			refreshUI();
		}
	}

	@Override
	public boolean isValid() {
		if (companyNameField.getText().length() < 2) {
			return false;
		}

		if (codeField.getText().length() != 2) {
			return false;
		}
		if (addressLine1Field.getText().length() < 2) {
			return false;
		}

		if (cityField.getText().length() < 2) {
			return false;
		}

		if (stateField.getText().length() < 2) {
			return false;
		}

		if (countryField.getText().length() < 2) {
			return false;
		}

		if (zipCodeField.getText().length() < 5) {
			return false;
		}

		if (telephone1Field.getText().length() < 5) {
			return false;
		}

		if (!ValidationUtilities.validateEmail(emailField.getText())) {
			return false;
		}

		return true;
	}

	@Override
	public String getValidationMessage() {

		if (companyNameField.getText().length() < 2) {
			return UIMessages.COMPANY_NAME_VALIDATION_MESSAGE;
		}

		if (codeField.getText().length() != 2) {
			return UIMessages.COMPANY_CODE_VALIDATION_MESSAGE;
		}
		if (addressLine1Field.getText().length() < 2) {
			return UIMessages.COMPANY_ADDRESS_VALIDATION_MESSAGE;
		}

		if (cityField.getText().length() < 2) {
			return UIMessages.COMPANY_CITY_VALIDATION_MESSAGE;
		}

		if (stateField.getText().length() < 2) {
			return UIMessages.COMPANY_STATE_VALIDATION_MESSAGE;
		}

		if (countryField.getText().length() < 2) {
			return UIMessages.COMPANY_COUNTRY_VALIDATION_MESSAGE;
		}

		if (zipCodeField.getText().length() < 5) {
			return UIMessages.COMPANY_ZIPCODE_VALIDATION_MESSAGE;
		}

		if (telephone1Field.getText().length() < 5) {
			return UIMessages.COMPANY_TELEPHONE_VALIDATION_MESSAGE;
		}
		if (!ValidationUtilities.validateEmail(emailField.getText())) {
			return UIMessages.COMPANY_EMAIL_VALIDATION_MESSAGE;
		}
		return null;

	}

	@Override
	public void refreshUI(Object... args) {
		if (args != null && args.length > 0) {
			Company company = (Company) args[0];
			this.company = company;
			updateMode = true;
			companyNameField.setText(company.getName());
			codeField.setText(company.getCompanyID());
			codeField.disableProperty().set(true);
			addressLine1Field.setText(company.getAddress1());
			addressLine2Field.setText(company.getAddress2());
			cityField.setText(company.getCity());
			stateField.setText(company.getState());
			countryField.setText(company.getCountry());
			zipCodeField.setText(company.getZipcode());
			telephone1Field.setText(company.getTelephone1());
			telephone2Field.setText(company.getTelephone2());
			faxField.setText(company.getFax());
			emailField.setText(company.getMailId());
		} else {
			updateMode = false;
			companyNameField.clear();
			codeField.clear();
			addressLine1Field.clear();
			addressLine2Field.clear();
			cityField.clear();
			stateField.clear();
			countryField.clear();
			zipCodeField.clear();
			telephone1Field.clear();
			telephone2Field.clear();
			faxField.clear();
			emailField.clear();
		}
	}
}
